package com.mlink.ai.chat.ads;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adsdk.android.ads.OxAdSdkManager;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class OxRemoteConfigHelper {

    @NonNull
    public static double[] getTopValuesConfig() {
        return new double[]{
                getDouble(OxRemoteConfigKeys.CONFIG_TOP10, 0.4),
                getDouble(OxRemoteConfigKeys.CONFIG_TOP20, 0.3),
                getDouble(OxRemoteConfigKeys.CONFIG_TOP30, 0.2),
                getDouble(OxRemoteConfigKeys.CONFIG_TOP40, 0.1),
                getDouble(OxRemoteConfigKeys.CONFIG_TOP50, 0.08),
                getDouble(OxRemoteConfigKeys.CONFIG_TOP60, 0.05),
        };
    }

    /**
     * 打点等级。
     */
    @EventLevel
    public static int getEventLevel() {
        // EnableDebug 时，强制设置 EventLevel 为 EventLevel.SDK。
        if (OxAdSdkManager.getInstance().isDebugEnabled()) return EventLevel.SDK;
        return getInt(OxRemoteConfigKeys.CONFIG_ADSDK_LOG_LEVEL, EventLevel.NONE);
    }

    /**
     * 获取深度用户阈值。
     */
    public static int getDepthUserLevel() {
        return getInt(OxRemoteConfigKeys.CONFIG_DEPTH_USER_LEVEL, 400);
    }


    /**
     * ltv medium 阈值。
     */
    public static double getAdLtvMedium() {
        return getDouble(OxRemoteConfigKeys.CONFIG_ADSDK_LTV_MEDIUM, 0.5);
    }

    /**
     * ltv high 阈值。
     */
    public static double getAdLtvHigh() {
        return getDouble(OxRemoteConfigKeys.CONFIG_ADSDK_LTV_HIGH, 2.0);
    }

    /**
     * 应用剩余可用内存低于该阈值时，不再展示广告，负数代表没有限制。
     */
    private static int getMemoryLimit(@AdFormat String adFormat) {
        String key = "meaningless_place_holder";
        switch (adFormat) {
            case AdFormat.BANNER:
                key = OxRemoteConfigKeys.CONFIG_ADSDK_MEMORY_LIMIT_BANNER;
                break;
            case AdFormat.MREC:
                key = OxRemoteConfigKeys.CONFIG_ADSDK_MEMORY_LIMIT_MREC;
                break;
            case AdFormat.INTERSTITIAL:
                key = OxRemoteConfigKeys.CONFIG_ADSDK_MEMORY_LIMIT_INTER;
                break;
            case AdFormat.REWARDED:
                key = OxRemoteConfigKeys.CONFIG_ADSDK_MEMORY_LIMIT_REWARDED;
                break;
            case AdFormat.NATIVE:
                key = OxRemoteConfigKeys.CONFIG_ADSDK_MEMORY_LIMIT_NATIVE;
                break;
        }
        return getInt(key, -1);
    }

    /**
     * 传给广告平台的关键字。
     */
    @NonNull
    public static List<String> getKeywords() {
        List<String> result = new ArrayList<>();
        String keywordsJson = getString(OxRemoteConfigKeys.CONFIG_ADSDK_KEYWORDS, null);
        if (TextUtils.isEmpty(keywordsJson)) return result;
        try {
            JSONArray jsonArray = new JSONArray(keywordsJson);
            for (int i = 0; i < jsonArray.length(); i++) {
                String keyword = jsonArray.getString(i);
                if (!TextUtils.isEmpty(keyword)) {
                    result.add(keyword);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Nullable
    private static String getString(@NonNull String key, String defaultValue) {
        String config = FirebaseRemoteConfig.getInstance().getString(key);
        return TextUtils.isEmpty(config) ? defaultValue : config;
    }

    private static double getDouble(@NonNull String key, double defaultValue) {
        double config = FirebaseRemoteConfig.getInstance().getDouble(key);
        return config == 0 ? defaultValue : config;
    }

    private static int getInt(@NonNull String key, int defaultValue) {
        int config = (int) FirebaseRemoteConfig.getInstance().getLong(key);
        return config == 0 ? defaultValue : config;
    }

    private static boolean getBool(@NonNull String key) {
        return FirebaseRemoteConfig.getInstance().getBoolean(key);
    }
}
